package com.l024.easysecurity.modules.sys.service.impl;

import com.l024.easysecurity.common.page.PageData;
import com.l024.easysecurity.common.page.PageParams;
import com.l024.easysecurity.common.utils.R;
import com.l024.easysecurity.common.utils.SpringBeanUtil;
import com.l024.easysecurity.modules.sys.dao.SysMenuMapper;
import com.l024.easysecurity.modules.sys.dao.SysRoleMapper;
import com.l024.easysecurity.modules.sys.entity.SysMenu;
import com.l024.easysecurity.modules.sys.entity.SysRole;
import com.l024.easysecurity.modules.sys.service.SysRoleService;
import com.l024.easysecurity.modules.sys.vo.sys.SysRoleVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SysRoleServiceImpl extends SysBaseService implements SysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public List<SysRole> getSysRoles() {
        return sysRoleMapper.getSysRoles();
    }

    @Override
    public List<SysRole> getRoleAllAndMenu() {
        List<SysRole> roleAllAndMenu = sysRoleMapper.getRoleAllAndMenu();
        if(roleAllAndMenu!=null&&roleAllAndMenu.size()>0){
            for(SysRole sysRole:roleAllAndMenu){
                List<SysMenu> menus = sysRole.getChildren();
                //封装每个菜单层级
                List<SysMenu> treeMenu = getTreeMenu(menus);
                //重新赋值
                sysRole.setChildren(treeMenu);
            }
            return roleAllAndMenu;
        }
        return null;
    }

    /**
     * 添加角色
     * @param sysRoleVo
     * @return
     */
    @Transactional
    @Override
    public R addRole(SysRoleVo sysRoleVo) {
        if(sysRoleVo!=null){
            String menuIds = sysRoleVo.getMenuIds();
            if(!StringUtils.isEmpty(menuIds)){
                //对象拷贝
                SysRole sysRole = new SysRole();
                SpringBeanUtil.copyPropertiesIgnoreNull(sysRoleVo,sysRole);
                //添加角色信息
                if(!StringUtils.isEmpty(sysRole.getName())
                        && !StringUtils.isEmpty(sysRole.getRemark())
                        && sysRole.getCreateUserId()>0){
                    //判断角色是否已经存在
                    SysRole sysRoleByName = sysRoleMapper.getSysRoleByName(sysRole.getName());
                    if(sysRoleByName!=null){
                        return R.error("该角色已经存在");
                    }
                    //添加角色
                    sysRoleMapper.addRole(sysRole);
                    System.out.println("添加的角色返回--->"+sysRole.getRoleId());
                    //添加权限
                    String[] menus = menuIds.split(","); // 用,分割
                    //批量添加
                    sysRoleMapper.addMenusByRole(sysRole.getRoleId(), menus);
                    return R.ok("添加成功");
                }else{
                    return R.error("角色名不可为null");
                }
                //添加权限相关
            }else{
                return R.error("请分配角色");
            }
        }
        return R.error("添加失败");
    }

    /**
     * 更新角色
     * @param sysRoleVo
     * @return
     */
    @Transactional
    @Override
    public R updateRole(SysRoleVo sysRoleVo) {
        if(sysRoleVo!=null&&sysRoleVo.getRoleId()>0){
            //更新角色信息
            sysRoleMapper.updateRoleById(sysRoleVo);
            //更新角色菜单
            String menuIds = sysRoleVo.getMenuIds();
            if(!StringUtils.isEmpty(menuIds)){
                //添加权限
                String[] menus = menuIds.split(","); // 用,分割
                //先查询一下角色id是否已经修改
                long roleId = sysRoleVo.getRoleId();
                List<Long> menuIdsByRoleId = sysRoleMapper.getMenuIdsByRoleId(roleId);
                String roleMenuIds = StringUtils.join(menuIdsByRoleId.toArray(), ",");     // 第二个参数为分隔符
                //菜单修改了才更新
                if(!menuIds.equals(roleMenuIds)){
                    //先删除角色下的所有权限，在添加
                    sysRoleMapper.delRoleAndMenu(roleId);
                    sysRoleMapper.addMenusByRole(roleId, menus);
                }
                return R.ok("修改成功");
            }else{
                return R.error("请分配权限");
            }
        }
        return R.error("更新失败");
    }

    @Transactional
    @Override
    public R delRole(long roleId) {
        if(roleId>0){
            //判断这个角色是否有绑定的用户
            Long hasUser = sysRoleMapper.selectRoleHasUser(roleId);
            if(hasUser>0){
                return R.error("该角色还存在绑定的用户");
            }
            //删除角色下绑定的菜单
            sysRoleMapper.delRoleAndMenu(roleId);
            //在删除角色信息
            sysRoleMapper.delRole(roleId);
            return R.ok("删除成功");
        }
        return R.error("角色id不存在");
    }

    /**
     * 角色条件分页查询
     * @param sysRole
     * @param pageParam
     * @return
     */
    @Override
    public PageData<SysRoleVo> selectPageSysRole(SysRole sysRole, PageParams pageParam) {
        List<SysRoleVo> sysRoles = sysRoleMapper.selectPageSysRoleAllMenu(sysRole, pageParam);
        Long count = sysRoleMapper.selectPageCount(sysRole);
        //查询每个角色下的菜单
        if(sysRoles!=null&&sysRoles.size()>0){
            for (SysRoleVo role:sysRoles){
                //根据角色查询菜单ids
                if(role!=null&&role.getRoleId()>0){
                    List<Long> menuIds = sysRoleMapper.getMenuIdsByRoleId(role.getRoleId());
                    role.setMenuIds(StringUtils.join(menuIds,","));
                }
            }
        }
        PageData<SysRoleVo> pageData =PageData.buildPage(sysRoles,count,pageParam.getPageSize(),pageParam.getPageNum());
        return pageData;
    }

    @Override
    public List<SysMenu> queryAllMenus() {
        List<SysMenu> sysMenus = sysMenuMapper.queryAllMenus();
        //封装每个菜单层级
        return getTreeMenu(sysMenus);
    }

}

